/*** 
 * @Author: Colin Dai
 * @Date: 2022-05-09 17:49:24
 * @LastEditors: Colin Dai
 * @LastEditTime: 2022-05-10 17:15:48
 * @FilePath: /jy-uos/src/softbus/interfaces/discovery_service.h
 * @Description: 
 * @dailianzheng@163.com
 * @Copyright (c) 2022 by Colin.Dai, All Rights Reserved. 
 */

#ifndef _DISCOVERY_SERVICE_H_
#define _DISCOVERY_SERVICE_H_

#ifdef __cplusplus
extern "C" {
#endif

//通用定义
#define MAX_DEV_IP_LEN 16
#define MAX_DEV_MAC_LEN 6
#define MAX_PACKAGE_NAME 64


//TODO：当前SDK版本和设备类型，需要定义号
#define CUR_SDK_VERSION      1
#define CUR_DEVICE_TYPE      LTU


/**
 * @brief Enumerates media, such as UDP, RS485 \HPLC and MESH, used for publishing services.
 *
 * Currently, the media can only be set to coap.
 *
 */
typedef enum {
    /** 未定义 */
    UNDEF = 0,
    /** UDP*/
    UDP = 1,
    /** RS485*/
    RS485 = 2,
    /** USB */
    HPLC = 3,
    /** MESH */
    MESH = 4,
} ExchangeMedium;


/**
 * @brief 设备发现频次，直接影响发现端的发现报文速率
 * This enumeration is involved only in Bluetooth and is not supported currently.
 */
typedef enum {
    /** Low */
    LOW = 0,
   /** Medium */
    MID = 1,
   /** High */
    HIGH = 2,
   /** Super-high */
    SUPER_HIGH = 3,
} ExchangeFreq;


/**
 * @brief 服务发布模式，主动模式就是直接广播报文发送出去，被动只是注册在本地总线，等待发现者来建联
 * The publishing service supports the passive mode. Lite devices support the passive mode only.
 */
typedef enum {
    /** Passive */
    DISCOVER_MODE_PASSIVE = 0x55,
    /** Proactive */
    DISCOVER_MODE_ACTIVE  = 0xAA
} DiscoverMode;




/**
 * @brief 定义发送给外界的总线上的服务列表
 *
 */
typedef struct PublishInfo {
  /** 服务ID，对内其实就是模块ID*/
    unsigned char publishId;
  /** 服务发布类型，主动还是被动 */
    DiscoverMode mode;
  /** 发布通道 */
    ExchangeMedium medium;
  /** 发布频次，仅mode为主动发布时有效 */
    ExchangeFreq freq;
  /** 单模块最多32个API暴露 */
    unsigned short capability;
} PublishInfo;


/**
 * @brief 发布服务失败原因.
 *
 * The failure reason is returned to the caller through the callback function.
 *
 */
typedef enum {
   /** Unsupported medium */
    PUBLISH_FAIL_REASON_NOT_SUPPORT_MEDIUM = 1,
   /** Invalid parameter */
    PUBLISH_FAIL_REASON_PARAMETER_INVALID = 2,
   /** Unknown reason */
    PUBLISH_FAIL_REASON_UNKNOWN = 0xFF
} PublishFailReason;



/**
 * @brief 设备类型定义
 *
 */
typedef enum DeviceTypeEnum {
    /** Unknown */
    UNKOWN = 0x00,
    /** Linux设备从01开始 */
    TTU = 0x01,
    /** MCU设备从11 开始 */
    LTU = 0x11,
} DeviceType;

/**
 * @brief 服务发布回调
 *
 */
typedef struct IPublishCallback {
    /** Callback for successful publishing */
    void (*onPublishSuccess)(int publishId);
    /** Callback for failed publishing */
    void (*onPublishFail)(int publishId, PublishFailReason reason);
} IPublishCallback;


/**
 * @brief 设备基本信息
 *
 */
typedef struct CommonDeviceInfo {   
    /** 设备ID,目前最多组网65535个设备 */
    unsigned short deviceId;
    /** 设备类型 */
    DeviceType deviceType;
    /** 当前SDK的版本 */
    unsigned char sdk_version;   
    /** 设备IP **/
    char deviceIp[MAX_DEV_IP_LEN];
    /** 设备MAC 二层通信时使用 */
    char deviceMac[MAX_DEV_MAC_LEN];
    /** 端口号,用于UDP时建立Seesion使用，其他介质不需要 */
    int opt_devicePort;
} CommonDeviceInfo;


/*** 
 * @brief 发布服务
 * @param {char} *moduleName
 * @param {PublishInfo*} info
 * @param {IPublishCallback} *cb
 * @return {*}
 */
int PublishService(const char *moduleName, const struct PublishInfo* info, const struct IPublishCallback *cb);

/*** 
 * @brief 取消服务
 * @param {char} *moduleName
 * @param {int} publishId
 * @return {*}
 */
int UnPublishService(const char *moduleName, int publishId);

#ifdef __cplusplus
}
#endif
#endif /* _DISCOVERY__SERVICE_H */